package com.heartware.sh.blackhole.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.Toast;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;
import com.heartware.sh.blackhole.ui.fragment.three.OneFragment;
import com.heartware.sh.blackhole.ui.fragment.three.ThreeFragment;
import com.heartware.sh.blackhole.ui.fragment.three.TwoFragment;

/**
 * Created by Administrator on 2016/11/29 0029.
 */
public class ThreeActivity extends BaseActivity{

    private FrameLayout fl;

    private FragmentTabHost tabHost;

    private Class[] fragments = {OneFragment.class, TwoFragment.class, ThreeFragment.class};

    private RadioButton rbOne, rbTwo, rbThree;
    private RadioGroup rg;

    private int menuId = R.menu.one;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getToolBarX().setTitle("ThreeActivity").setNavigationIcon(R.mipmap.ic_launcher).setDisplayHomeAsUpEnabled(true);
//        getToolBarX().setTitle("").setCustomView(LayoutInflater.from(mContext).inflate(R.layout.title_common, null)).hide();
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_three;
    }


    @Override
    public void initView() {
        super.initView();

        fl = (FrameLayout) findViewById(R.id.fl);

        rbOne = (RadioButton) findViewById(R.id.rb_one);
        rbTwo = (RadioButton) findViewById(R.id.rb_two);
        rbThree = (RadioButton) findViewById(R.id.rb_three);

        rg = (RadioGroup) findViewById(R.id.rg);

        tabHost = (FragmentTabHost) findViewById(R.id.tabHost);
        tabHost.setup(mContext, getSupportFragmentManager(), R.id.fl);

        for (int i = 0 ; i < fragments.length; i ++){
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(String.valueOf(i)).setIndicator(String.valueOf(i));

            tabHost.addTab(tabSpec, fragments[i], null);
        }

        tabHost.setCurrentTab(0);


    }

    @Override
    public void setListener() {
        super.setListener();

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i){
                    case R.id.rb_one:
                        tabHost.setCurrentTab(0);
                        menuId = R.menu.one;
                        break;
                    case R.id.rb_two:
                        tabHost.setCurrentTab(1);
                        menuId = -1;
                        break;
                    case R.id.rb_three:
                        tabHost.setCurrentTab(2);
                        menuId = R.menu.three;
                        break;
                    default:
                        break;
                }

                //刷新菜单界面...
                invalidateOptionsMenu();
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        if(menuId == -1){
            menu.clear();
        }else{
            getMenuInflater().inflate(menuId, menu);
        }

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //menu点击时间...

//        Toast.makeText(mContext, "==="+item.getItemId()+"===", Toast.LENGTH_SHORT).show();
        switch (item.getItemId()){
            /** 标题栏返回按钮的默认ID为 android.R.id.home */
            case android.R.id.home:
                finish();
                break;
            case R.id.action_search:
                Toast.makeText(mContext, "search", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_more:
                Toast.makeText(mContext, "more", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }
}
